//
//  STOrderCommentView.m
//  YiYueHairdresser
//
//  Created by 路人甲 on 2018/11/7.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STOrderCommentView.h"
#import "STUserInfoView.h"
#import "STDoorEvaCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

@implementation STOrderCommentView


-(void)setOrderCommentsListView{
    if (!orderCommentView) {
        orderCommentView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        orderCommentView.delegate = self;
        orderCommentView.dataSource = self;
        orderCommentView.separatorStyle = UITableViewCellSelectionStyleNone;
        orderCommentView.backgroundColor = [UIColor clearColor];
        orderCommentView.tableFooterView = [UIView new];
        [orderCommentView registerClass:[STCommentCell class] forCellReuseIdentifier:@"STCommentCell"];
        [self addSubview:orderCommentView];
    }else{
        [orderCommentView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 200;
    }else if (indexPath.section == 1 ){
        return 150;
    }else if (indexPath.section == 2 ){
        float height = [tableView fd_heightForCellWithIdentifier:@"STCommentCell" configuration:^(id cell) {
            [self configureCell:cell atIndexPath:indexPath];
        }];
        return height;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell;
    if (indexPath.section == 0 ){
        cell = [tableView dequeueReusableCellWithIdentifier:@"commonCell"];
        if(!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"commonCell"];
        }
        [cell.contentView removeAllSubviews];
        //加载用户信息
        STUserInfoView * userView =  [[NSBundle mainBundle] loadNibNamed:@"STUserInfoView"owner:self options:nil].lastObject;
        userView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
        [userView setUserInfoWithData:self.detailModel];
        [cell.contentView addSubview:userView];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 1) {
        static NSString * evaIdentifier = @"evaIdentifier";
        STDoorEvaCell * cell =[tableView dequeueReusableCellWithIdentifier:evaIdentifier];
        if (!cell) {
            cell = [[STDoorEvaCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:evaIdentifier];
        }
        [cell setEvaluateView:self.totalScore];
        cell.commentBtn.hidden = YES;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 2 ){
        STCommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"STCommentCell"];
        commentCell.isOrderCommon = YES;
        commentCell = (STCommentCell *)[self configureCell:commentCell atIndexPath:indexPath];
        commentCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return commentCell;
    }
    return cell;
}


- (UITableViewCell *)configureCell:(STCommentCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"STCommentCell";
    if (!cell) {
        cell = [[STCommentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.isOrderCommon = YES;
    }
    cell.delegate = self;
    [cell setCommentWithData:self.orderCommentData];
    
    return cell;
}

//cell 回调
-(void)commentCell:(STCommentCell *)cell replayCommentText:(NSString *)replayStr{
    if(!replayStr.length){
        return;
    }
    if ([self.delegate respondsToSelector:@selector(orderCommentsView:indexpath:replayCommentText:)]) {
        NSIndexPath * indepath = [orderCommentView indexPathForCell:cell];
        [self.delegate orderCommentsView:self indexpath:indepath replayCommentText:replayStr];
    }
}




@end
